$().ready(function() {
	validateRule();

	var bigClass = $("#bigClass").val();
	if(bigClass == '作品展览') {
		/*$("#div1").show();*/
		$("#div2").show();
		$("#span1").html("文章前言：");
		$("#span2").html("文章作者：");
		$("#span3").html("创作过程：");
	}else {
		/*$("#div1").hide();*/
		$("#div2").hide();
		$("#span1").html("文章介绍：");
		$("#span2").html("作者介绍：");
		$("#span3").html("文章内容：");
	}

	$('.summernote').summernote({
		height : '220px',
		lang : 'zh-CN',
		callbacks: {
			onImageUpload: function(files, editor, $editable) {
				sendFile(files);
			}
		}
	});

	var articleContent = $("#articleContent").val();
	$('#content_sn').summernote('code', articleContent);

});

$.validator.setDefaults({
	submitHandler : function() {
		update();
	}
});
function update() {
	if($('#content_sn').summernote('isEmpty')) {
		parent.layer.alert("请输入文章内容")
		return false;
	}
	var content_sn = $("#content_sn").summernote('code');
	$("#articleContent").val(content_sn);
	//判断修改也是否上传图片，没有上传图片则把oldAttachId值放到新的attachId中去
	var attachId = $("#attachId").val();
	var flag = judeImage(attachId);
	if(!flag) {
		$("#attachId").val($("#oldAttachId").val());
	}
	var secondAttachId = $("#secondAttachId").val();
	var flag1 = judeImage(secondAttachId);
	if(!flag1) {
		$("#secondAttachId").val($("#oldSecondAttachId").val());
	}
	var rotationList = [];
	for(var i=0;i<$(".rotationList li").length;i++){
		var FileShowDO = {};
		FileShowDO.name = $('.rotationList li').eq(i).find('span').text();
		FileShowDO.src = $('.rotationList li').eq(i).find('img').attr('src');
		FileShowDO.value = $('.rotationList li').eq(i).find('input').val();
		rotationList.push(FileShowDO);
	}
	$.ajax({
		cache : true,
		type : "POST",
		url : "/system/article/update?"+$('#signupForm').serialize(),
		//data : $('#signupForm').serialize(),// 你的formid
		data : {
			rotationList:JSON.stringify(rotationList),
		},
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg("操作成功");
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);

			} else {
				parent.layer.alert(data.msg)
			}
		}
	});

}
function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
			bigClass : {
				required : true
			},
			smallClass : {
				required : true
			},
			title : {
				required : true,
				maxlength: 100
			},
			/*articleIntroduction : {
				required : true,
				maxlength: 200
			},
			authorIntroduction : {
				required : true,
				maxlength: 200
			},*/
			articleContent : {
				required : true
			}
		},
		messages : {
			bigClass : {
				required : icon + "请选择文章大类"
			},
			smallClass : {
				required : icon + "请选择文章小类"
			},
			title : {
				required : icon + "请输入文章标题",
				maxlength: icon + "文章标题不能超过一百个字符",
			},
			/*articleIntroduction : {
				required : icon + "请输入文章简介",
				maxlength: icon + "文章简介不能超过两百个字符",
			},
			authorIntroduction : {
				required : icon + "请输入作者简介",
				maxlength: icon + "作者简介不能超过两百个字符",
			},*/
			articleContent : {
				required : icon + "请输入文章内容"
			}
		}
	})
}

selectSmallClass();

//根据文章大类选择小类
function selectSmallClass() {
	var bigClass = $("#bigClass").val();
	if(!bigClass) {
		var html = '<option value="">'+"请选择"+'</option>';
		$("#smallClass").val("");
		$("#classId").html(html);
		parent.layer.alert("请选择文章大类");
		return false;
	}
	if(bigClass == '作品展览') {
		/*$("#div1").show();*/
		$("#div2").show();
		$("#span1").html("文章前言：");
		$("#span2").html("文章作者：");
		$("#span3").html("创作过程：");
	}else {
		/*$("#div1").hide();*/
		$("#div2").hide();
		$("#span1").html("文章介绍：");
		$("#span2").html("作者介绍：");
		$("#span3").html("文章内容：");
	}
	$.ajax({
		type : "POST",
		url : "/system/article/getSmallClass",
		data : {"bigClass":bigClass},
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				if(data.data != null && data.data != null) {
					var classId = $("#classIds").val();
					$.each(data.data, function(i){
						if(i == 0) {
							$("#smallClass").val(this.smallClass);
						}
						if(classId == this.id) {
							html += '<option value="'+this.id+'" selected>'+this.smallClass+'</option>';
						}else {
							html += '<option value="'+this.id+'">'+this.smallClass+'</option>';
						}
					});
				}
				$("#classId").html(html);
			}
		}
	});
}

function getSmallClassValue() {
	var smallClass = $('#classId option:selected').text();
	$("#smallClass").val(smallClass);
}

function judeImage(attachId) {
	var flag = false;
	$.ajax({
		type : "POST",
		url :  ctx + "/common/sysFile/list",
		data : {attachId:attachId},
		async : false,
		success : function(data) {
			if(data != null && data != '') {
				flag = true;
			}
		}
	});
	return flag;
}